package com.xc.videosample

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.feo.kpler.FKerVideoProgressListener
import com.xc.videosample.databinding.FragmentVideoPlayerBinding

class VideoPlayerFragment : Fragment() {

    companion object {
        fun newInstance(int: Int): VideoPlayerFragment {
            val arguments = Bundle().apply {
                putInt("index", int)
            }
            val fragment = VideoPlayerFragment()
            fragment.arguments = arguments
            return fragment
        }
    }

    private lateinit var dataBinding: FragmentVideoPlayerBinding
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        dataBinding = DataBindingUtil.inflate<FragmentVideoPlayerBinding>(
            inflater,
            R.layout.fragment_video_player,
            container,
            false
        )

        return dataBinding.root
    }

    var videoUri =
        "http://baobab.kaiyanapp.com/api/v1/playUrl?vid=2178&editionType=default&source=aliyun"

    private var index = 0
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        index = arguments?.getInt("index")?:0
        //        videoUri = "http://127.0.0.1:34603/https%3A%2F%2Fcdn.gzzhitui.com%2Fjiliang-bucket%252Fcreative%252Fhuiyuan-hpageicon%252Fqumiao%252F1.%25E6%25B0%2591%25E9%2597%25B4%25E6%2595%2585%25E4%25BA%258B10s.mp4"

//        val url = Uri.parse("android.resource://" + packageName + "/" + R.raw.test_kekyboard)
//        dataBinding.playerView.start(videoUri)

        dataBinding.playerView.registerProgressListener(object : FKerVideoProgressListener {
            override fun start() {
                Log.i("chyy", "start: ")
            }

            override fun onProgress(currentPosition: Int, duration: Int) {
//                Log.i("chyy", "onProgress: $currentPosition  duration: $duration")
            }

            override fun complete() {
                Log.i("chyy", " ---------- ****** complete")
                dataBinding.playerView.replay()
            }

        })
        if (index == 1) {
            videoUri = "https://cdn.gzzhitui.com/jiliang-bucket/kbd/article/img/cq78gi2f0va7n9okavig.mp4"
        }

        if (index == 2) {
            videoUri =
                "https://cdn.gzzhitui.com/jiliang-bucket/kbd/article/img/cq6i9p3lt3d10ceo3rgg.mp4"
        }


        if (index == 3) {
            videoUri =
                "https://cdn.gzzhitui.com/jiliang-bucket/kbd/article/img/cq7kqim26iam9o8anak0.mp4"
        }


        dataBinding.playerView.setCoverUrl("http://gips2.baidu.com/it/u=195724436,3554684702&fm=3028&app=3028&f=JPEG&fmt=auto?w=1280&h=960")
        dataBinding.playerView.setUseCache(true)
        dataBinding.playerView.showController(true)
        dataBinding.playerView.start(videoUri)

//        dataBinding.playerView.startUri(url)
//        dataBinding.playerView.videoView.seekTo(3)
        dataBinding.playerView.postDelayed(
            { dataBinding.playerView.visibility = View.VISIBLE },
            15
        )



        dataBinding.btnStart.setOnClickListener {
            dataBinding.playerView.start(videoUri)
        }
        dataBinding.btnPause.setOnClickListener {

            dataBinding.playerView.activePause()
        }

        dataBinding.btnResume.setOnClickListener {
            dataBinding.playerView.activeResume()
        }
        dataBinding.btnReplay.setOnClickListener {
            dataBinding.playerView.replay()

        }



        dataBinding.btnSoundOff.setOnClickListener {
            dataBinding.playerView.soundOff()
        }
        dataBinding.btnSoundOn.setOnClickListener {
            dataBinding.playerView.soundOn()
        }


    }

    override fun onResume() {
        super.onResume()
        Log.i("chyy", " VideoPayerFragment  onResume id: ${hashCode()}")
    }

    override fun onPause() {
        super.onPause()
        Log.i("chyy", " VideoPayerFragment  onPause ${hashCode()}")
    }

}